<template>
  <section class="component case-excel-import-new">
    <el-form ref="form" :model="importModel" label-width="150px" :rules="rules">
      <el-form-item label="上传文件">
        <file-upload
          ref="upload_case_import"
          :fileList.sync="fileList"
          :fileNumberLimit="1"
          :AllowExtension="['xlsx']"
        ></file-upload>
      </el-form-item>
    </el-form>
    <div class="operate-buttons">
      <el-button @click="emitClose">取消</el-button>
      <el-button @click="submit">确定</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import { Dependencies } from "~/core/decorator";
import { State, Getter, Action, namespace } from "vuex-class";
import { Emit, Component, Prop, Watch } from "vue-property-decorator";
import FileUpload from "~/components/common/file-upload.vue";
import { CustomerGroupManageService } from "~/services/domain-service/customer-group-manage.service";
import { PageService } from "~/utils/page.service";
import { ListRemovedService } from "~/services/domain-service/list-removed.service";
import { importMenuType } from "~/config/enum.config";

@Component({
  name: "case-excel-import",
  components: {
    FileUpload
  }
})
export default class CaseExcelImportNew extends Vue {
  @State private principalList: any;
  @Dependencies(CustomerGroupManageService)
  private customerGroupManageService: CustomerGroupManageService;
  @Dependencies(ListRemovedService)
  private listRemovedService: ListRemovedService;
  @Prop() private submitType;
  private importModel: any = {};
  private rules: any = {};
  private fileList: any = [];
  @Emit("close")
  private emitClose() {
    const upload: any = this.$refs["upload_case_import"];
    upload.reset();
    const dataform: any = this.$refs["form"];
    dataform.resetFields();
  }

  @Emit("success")
  private emitSuccess() {
    this.emitClose();
  }
  // @Watch('submitType', { immediate: true })
  // private change () {
  //   console.log(this.submitType)
  // }
  private submit() {
    if (this.fileList.length === 0) {
      this.$message("请上传导入文件");
      return;
    }
    // 存储文件ID
    const fileId = this.fileList[0].response.id;
    const dataForm: any = this.$refs["form"];
    dataForm.validate(valid => {
      if (!valid) return;
    });
    if (this.submitType === importMenuType.accountTag)
      this.accountSubmit(fileId);
    if (this.submitType === importMenuType.coustomerTag)
      this.coustomerSubmit(fileId);
    if (this.submitType === importMenuType.listRemoved) this.listRem(fileId);
  }

  private listRem(fileId) {
    this.listRemovedService.importEliminate({ id: fileId }).subscribe(data => {
      // if (data.isSuccess === "NO") this.$common.downloadFile(data.url);
      if (data.url) {
        this.$common.downloadFile(data.url)
      } else {
        this.$alert('上传成功', '上传名单剔除', {
          confirmButtonText: '确定'
        })
      }
      this.emitSuccess();
    });
  }
  
  private accountSubmit(fileId) {
    this.customerGroupManageService
      .caseTagBatchAdjust({ fileId })
      .subscribe(data => {
        // if (data.isSuccess !== "YES") this.$common.downloadFile(data.url);
        // this.emitSuccess();
        if(!data.url) {
          this.$message.success("上传成功")
          this.emitSuccess();
        } else {
          this.$common.downloadFile(data.url);
          this.emitSuccess();
        }
      });
  }

  private coustomerSubmit(fileId) {
    this.customerGroupManageService
      .personalTagBatchAdjust({ fileId })
      .subscribe(data => {
        // if (data.isSuccess !== "YES") this.$common.downloadFile(data.url);
        // this.emitSuccess();
        if(!data.url) {
          this.$message.success("上传成功")
          this.emitSuccess();
        } else {
          this.$common.downloadFile(data.url);
          this.emitSuccess();
        }
      });
  }
}
</script>
